#include "Map.h"



Map::Map( short size_x  , short size_y  )
{
    m_size_x = size_x ;
    m_size_y = size_y ;

    for (short i(0); i < m_size_x ; ++i )
    {
        std::vector< std::pair< short, Unit*> > temp_vec ;

        for (short j(0); j < m_size_y ; ++j)
        {
            Unit * u = NULL ;

            temp_vec.push_back( std::make_pair(1,u)) ;

        }

        m_map.push_back(temp_vec);
        temp_vec.clear();
    }

    //ctor
}

Unit* Map::getUnitAt(short x,short y) const{
    if (x >= 0 && x < m_size_x && y>=0 && y < m_size_y )
        return m_map[x][y].second ;

    std::cout << "ERROR : map out of range accessing unit at " << x << " " << y << std::endl ;
    return NULL ;
};

short Map::getGroundAt(short x,short y) const {
    if (x >= 0 && x < m_size_x && y>=0 && y < m_size_y )
        return m_map[x][y].first ;

    std::cout << "map out of range accessing ground at " << x << " " << y << std::endl ;
    return 500 ; // En dehors du terrain : case inaccessible (on evite de faire des calculs dessus)
};

bool Map::setUnitAt(short x,short y, Unit* newunit ){
    if (x >= 0 && x < m_size_x && y>=0 && y < m_size_y )
    {
        m_map[x][y].second = newunit ;
        return true ;
    }
    std::cout << "map out of range setting unit at " << x << " " << y << std::endl ;
    return false;
};

bool Map::setUnitAt(sf::Vector2i pos, Unit* newunit ){
    return setUnitAt(pos.x,pos.y,newunit);
};

bool Map::setGroundAt(short x,short y, short newground ){
    if (x >= 0 && x < m_size_x && y>=0 && y < m_size_y )
    {
        m_map[x][y].first = newground ;
        return true ;
    }
    std::cout << "map out of range setting ground at " << x << " " << y << std::endl ;
    return false;
};

Map::~Map()
{
    for (short i(0); i < m_size_x ; ++i )
    {
        for (short j(0); j < m_size_y ; ++j)
        {
            delete m_map[i][j].second ;
        }
    }
    //dtor
}
